package com.iris.live.services.report.sources;

import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.BaseReportDataSource;
import com.iris.live.services.report.data.ReportContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/8/24.
 */
@Component("CRMGroupSurveyReportSource")
public class CRMGroupSurveyReportSource extends BaseReportDataSource {
    private Logger logger = LoggerFactory.getLogger(CRMGroupSurveyReportSource.class);

    @Override
    public List getData(ReportContext context, List<ReportConfig> config) throws Exception {
        List list = new ArrayList();
        if (context != null && context.getParams() != null) {
            Map info = context.getParams();
            String displayType = null, resType = null, selectYear = null, selMonth = null,
                    selRegionCode = null, selPMACode = null, selDealerCode = null, queVersion = null;
            //校验 参数 纬度；
            if (StringUtils.isNullOrBlank(info.get("displayType"))) {
                logger.error("crm调研报表--参数 纬度 为必填参数");
                return list;
            }
            displayType = info.get("displayType").toString();
            if ((!"1".equals(displayType) && !"2".equals(displayType))) {
                logger.error("crm调研报表--参数 纬度 不在值域内，displayType--" + displayType);
                return list;
            }

            //校验 参数 调研类型
            resType = info.get("resType") == null?"":info.get("resType").toString();
            if(StringUtils.isNullOrBlank(resType) || "all".equalsIgnoreCase(resType) || "全部".equalsIgnoreCase(resType)){
                resType = null;
            }

            //校验 参数 问卷版本号
            queVersion = info.get("queVersion") == null?"":info.get("queVersion").toString();
            if(StringUtils.isNullOrBlank(queVersion) || "all".equalsIgnoreCase(queVersion) || "全部".equalsIgnoreCase(queVersion)){
                queVersion = null;
            }

            //校验 参数 年份
            selectYear = info.get("selectYear") == null?"":info.get("selectYear").toString();
            if(StringUtils.isNullOrBlank(selectYear) || "all".equalsIgnoreCase(selectYear) || "全部".equalsIgnoreCase(selectYear)){
                selectYear = null;
            }

            //获取 参数 月份
            selMonth = info.get("selMonth") == null?"":info.get("selMonth").toString();
            if(StringUtils.isNullOrBlank(selMonth) || "all".equalsIgnoreCase(selMonth) || "全部".equalsIgnoreCase(selMonth)){
                selMonth = null;
            } else {
                if (selMonth.contains("月")) {
                    selMonth.replace("月", "");
                }
                if (selMonth.length() == 1) {
                    selMonth = "0" + selMonth;
                }
            }

            //校验 参数 大区
            selRegionCode = info.get("selRegionCode") == null?"":info.get("selRegionCode").toString();
            if(StringUtils.isNullOrBlank(selRegionCode) || "all".equalsIgnoreCase(selRegionCode) || "全部".equalsIgnoreCase(selRegionCode)){
                selRegionCode = null;
            }

            // 校验 参数 小区
            selPMACode = info.get("selPMACode") == null?"":info.get("selPMACode").toString();
            if(StringUtils.isNullOrBlank(selPMACode) || "all".equalsIgnoreCase(selPMACode) || "全部".equalsIgnoreCase(selPMACode)){
                selPMACode = null;
            }

            //校验 参数 经销商
            selDealerCode = info.get("selDealerCode") == null?"":info.get("selDealerCode").toString();
            if(StringUtils.isNullOrBlank(selDealerCode) || "all".equalsIgnoreCase(selDealerCode) || "全部".equalsIgnoreCase(selDealerCode)){
                selDealerCode = null;
            }

            list = getFirstSurveyReportData(displayType, resType, selectYear, selMonth, selRegionCode, selPMACode, selDealerCode, queVersion);
        }
        return list;
    }

    //首次离店处调研查询
    private List getFirstSurveyReportData(String type, String resType, String selectYear, String selMonth, String selRegionCode,
                                          String selPMACode, String selDealerCode, String queVersion) {
        StringBuffer sql = new StringBuffer();
        Map params = new HashMap();
        params.put("resType", resType);
        params.put("selectYear", selectYear);
        params.put("queVersion", queVersion);
        sql.append(" SELECT  ");
        if ("1".equals(type)) {
            sql.append("    a.count_month date, ");
        } else {
            sql.append("   'DCH' 集团,b.许可_LSH,b.大区 AS 大区, b.大区_EN AS 大区_EN, b.小区 AS 小区, b.小区_EN AS 小区_EN, ");
            sql.append(" IF (isnull(b.CTA) OR b.CTA = '', '其他cta', b.CTA ) AS CTA, ");
            sql.append(" IF (isnull(b.CTA_EN) OR b.CTA_EN = '', 'Other CTA', b.CTA_EN ) AS CTA_EN, ");
            sql.append(" b.许可_BMBS AS 许可_BMBS, ");
        }
        //根据调研类型获取 查询sql返回结果集
        sql.append(getBaseSql(resType));

        sql.append(" from vw_group_report_crm_survey_count a ");
        sql.append(" JOIN lsh经销商表 b ON a.permit = b.`许可_BMBS` ");
        sql.append(" WHERE a.stage = :resType AND a.count_year = :selectYear AND (a.version = :queVersion ");
        if ("01".equals(resType)) {
            sql.append(" OR a.version = 'firstSurveyTarget' ");
        } else if ("02".equals(resType)) {
            sql.append(" OR a.version = 'sleepSurveyTarget' ");
        } else if ("03".equals(resType)) {
            sql.append(" OR a.version = 'failSurveyTarget' ");
        } else if ("04".equals(resType)) {
            sql.append(" OR a.version = 'NPSSurveyTarget' ");
        }
        sql.append("  ) ");
        if (!StringUtils.isNullOrBlank(selRegionCode)) {
            sql.append(" AND b.大区_EN = :selRegionCode ");
            params.put("selRegionCode", selRegionCode);
        }
        if (!StringUtils.isNullOrBlank(selPMACode)) {
            sql.append(" AND b.CTA_EN = :selPMACode ");
            params.put("selPMACode", selPMACode);
        }
        if ("1".equals(type)) {
            if (!StringUtils.isNullOrBlank(selDealerCode)) {
                sql.append(" AND b.许可_BMBS = :selDealerCode ");
                params.put("selDealerCode", selDealerCode);
            }
            sql.append(" GROUP BY a.count_month ");
        } else {
            if (!StringUtils.isNullOrBlank(selMonth)) {
                sql.append(" AND a.count_month = :selMonth ");
                params.put("selMonth", selMonth);
            }
            sql.append(" GROUP BY a.permit ");
        }
        return this.getList(sql.toString(), params);
    }

    private String getBaseSql (String resType) {
        StringBuffer sql = new StringBuffer();
        if ("01".equals(resType)) {
            sql.append(" 	sum(IF(a.stage = '01' AND a.count_type = '目标调研客户数', a.count_num, 0)) 首次离店调研报告_目标调研客户数, ");
            sql.append(" 	sum(IF(a.stage = '01' AND a.count_type = '已处理数', a.count_num, 0)) 首次离店调研报告_已处理数, ");
            sql.append(" 	sum(IF(a.stage = '01' AND a.count_type = '无效电话数', a.count_num, 0)) 首次离店调研报告_无效电话数, ");
            sql.append(" 	sum(IF(a.stage = '01' AND a.count_type = '重新分配数', a.count_num, 0)) 首次离店调研报告_重新分配数,  ");
            sql.append(" 	sum(IF(a.count_type = '对销售顾问满意数', a.count_num, 0)) 首次离店调研报告_对销售顾问满意数,  ");
            sql.append(" 	sum(IF(a.count_type = '邀约试乘试驾数', a.count_num, 0)) 首次离店调研报告_邀约试乘试驾数,  ");
            sql.append(" 	sum(IF(a.count_type = '离店后未跟进数', a.count_num, 0)) 首次离店调研报告_离店后未跟进数 ");
        } else if ("02".equals(resType)) {
            sql.append(" 	sum(IF(a.stage = '02' AND a.count_type = '目标调研客户数', a.count_num, 0)) 休眠调研报告_目标调研客户数, ");
            sql.append(" 	sum(IF(a.stage = '02' AND a.count_type = '已处理数', a.count_num, 0)) 休眠调研报告_已处理数, ");
            sql.append(" 	sum(IF(a.stage = '02' AND a.count_type = '无效电话数', a.count_num, 0)) 休眠调研报告_无效电话数, ");
            sql.append(" 	sum(IF(a.stage = '02' AND a.count_type = '重新分配数', a.count_num, 0)) 休眠调研报告_重新分配数,  ");
            sql.append(" 	sum(IF(a.count_type = '销售顾问未跟进数', a.count_num, 0)) 休眠调研报告_销售顾问未跟进数,  ");
            sql.append(" 	sum(IF(a.count_type = '有效客户数', a.count_num, 0)) 休眠调研报告_有效客户数,  ");
            sql.append(" 	sum(IF(a.count_type = '确认战败数', a.count_num, 0)) 休眠调研报告_确认战败数 ");
        } else if ("03".equals(resType)) {
            sql.append(" 	sum(IF(a.stage = '03' AND a.count_type = '目标调研客户数', a.count_num, 0)) 战败调研报告_目标调研客户数, ");
            sql.append(" 	sum(IF(a.stage = '03' AND a.count_type = '已处理数', a.count_num, 0)) 战败调研报告_已处理数, ");
            sql.append(" 	sum(IF(a.stage = '03' AND a.count_type = '无效电话数', a.count_num, 0)) 战败调研报告_无效电话数, ");
            sql.append(" 	sum(IF(a.stage = '03' AND a.count_type = '重新分配数', a.count_num, 0)) 战败调研报告_重新分配数,  ");
            sql.append(" 	sum(IF(a.count_type = '战败数', a.count_num, 0)) 战败调研报告_战败数,  ");
            sql.append(" 	sum(IF(a.count_type = '战败_已购车数', a.count_num, 0)) 战败调研报告_战败_已购车数  ");
        } else if ("04".equals(resType)) {
            sql.append(" 	sum(IF(a.stage = '04' AND a.count_type = '目标调研客户数', a.count_num, 0)) NPS调研报告_目标调研客户数, ");
            sql.append(" 	sum(IF(a.stage = '04' AND a.count_type = '已处理数', a.count_num, 0)) NPS调研报告_已处理数, ");
            sql.append(" 	sum(IF(a.count_type = '推荐名单推送数', a.count_num, 0)) NPS调研报告_推荐名单推送数,  ");
            sql.append(" 	sum(IF(a.count_type = '有效样本数', a.count_num, 0)) NPS调研报告_有效样本数,  ");
            sql.append(" 	sum(IF(a.count_type = '销售净客户推荐值', a.count_num, 0)) NPS调研报告_销售净客户推荐值 ");
        }
        return sql.toString();
    }
}
